package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.ProjectGlobalVar;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.base.BaseParam;
import com.jucai.pppeiyouclient.comm.CommonData;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.CommonUtil;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.GetVerTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

import static com.jucai.pppeiyouclient.R.id.register_agree;
import static com.jucai.pppeiyouclient.R.id.register_agreement;
import static com.jucai.pppeiyouclient.R.id.register_login;
import static com.jucai.pppeiyouclient.R.id.register_next;

public class RegisterFirstStepActivity extends BaseActivity {
    private static final String TAG = "RegisterFirstStepActivity : ";
    private EditText registerPhone, registerVer, registerPass;
    private TextView registerAgreement, registerLogin, registerNext;
    private GetVerTextView registerGetVer;
    private CheckBox registerAgree;
    private String isEmpty = "";

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_first_step);
        ((TextView) findViewById(R.id.common_title_text)).setText("注册");

        registerPhone = (EditText) findViewById(R.id.register_phone);
        registerVer = (EditText) findViewById(R.id.register_ver);
        registerPass = (EditText) findViewById(R.id.register_pass);
        registerGetVer = (GetVerTextView) findViewById(R.id.register_get_ver);
        registerAgreement = (TextView) findViewById(register_agreement);
        registerLogin = (TextView) findViewById(register_login);
        registerNext = (TextView) findViewById(register_next);
        registerAgree = (CheckBox) findViewById(register_agree);
    }

    @Override
    protected void initData() {
        /**
         * 设置下划线
         */
        registerLogin.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        registerPhone.setSingleLine();
        registerVer.setSingleLine();
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        registerGetVer.setOnClickListener(this);
        registerAgreement.setOnClickListener(this);
        registerLogin.setOnClickListener(this);
        registerNext.setOnClickListener(this);
    }

    private String judgeEmpty() {
        if (registerPhone.getText().toString().equals("")) {
            return "手机号不能为空";
        } else if (registerVer.getText().toString().equals("")) {
            return "验证码不能为空";
        } else if (registerPass.getText().toString().equals("")) {
            return "密码不能为空";
        } else {
            return "";
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(RegisterFirstStepActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            /**
             * 获取验证码
             */
            case R.id.register_get_ver:
                //是否第一次注册该号码，是则请求验证码，否则提示已经注册
                if (registerPhone.getText().toString().equals("")) {
                    ToastUtils.showToast("请输入手机号");
                } else if (registerGetVer.getIsHandler()) {
                    return;
                } else {
                    HashMap<String, String> verMap = new HashMap<>();
                    verMap.put("member_phone", registerPhone.getText().toString());
                    verMap.put("service", "Public_VerifyCode.SendRegCode");
                    CommonRequestNetData.getJsonStringData(RegisterFirstStepActivity.this, verMap, true, new CommonRequestNetData.JsonStringCallBack() {
                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e(TAG + "str = " + str);
                            ToastUtils.showToast(str);
                            if (!str.equals("该用户已经被注册")) {
                                registerGetVer.setHandler();
                            }

                        }
                    });
                }
                break;

            case R.id.register_next:
                CommonUtil.hideSoftInput(registerPass);
                //第一次注册，填完整信息跳转下一个注册界面，如果正在审核跳转审核界面，未通过跳转未通过界面，
                // 已注册成功提示已被注册，点击登录进行登录，需要状态码
                isEmpty = judgeEmpty();
                if (isEmpty.equals("")) {
                    if (registerAgree.isChecked()) {
                        if (registerPass.getText().toString().length() < 8) {
                            ToastUtils.showToast("密码至少要设置8位喔~");
                            return;
                        }
                        HashMap<String, String> param = new HashMap<>();
                        param.put("service", "Accompany_Register.go");
                        param.put("member_phone", registerPhone.getText().toString());
                        param.put("password", registerPass.getText().toString());
                        param.put("verifycode", registerVer.getText().toString());
                        param.put("member_register_from", "android");
                        param.put("member_extension_id", LoginUtils.getOpenInstallId(RegisterFirstStepActivity.this));
                        param.put("member_comfrom", LoginUtils.getOpenInstallChannel(RegisterFirstStepActivity.this));
                        param.put("jpush_regid", JPushInterface.getRegistrationID(this));

                        CommonRequestNetData.getJsonObjectData(RegisterFirstStepActivity.this, param, true, new CommonRequestNetData.JsonObjectCallBack() {
                            @Override
                            public void parseJsonObject(JSONObject jsonObject) {
                                LogUtils.e(TAG + "jsObj = " + jsonObject);
                                ToastUtils.showToast("注册成功");
                                try {
                                    String token = jsonObject.getString("token");
                                    String memberId = jsonObject.getString("member_id");
                                    String member_avatar = jsonObject.getString("member_avatar");

                                    String rongyun_token = jsonObject.has("rongyun_token") ? jsonObject.getString("rongyun_token") : "";
                                    LogUtils.e(TAG + "token = " + token + " , memberId = " + memberId + ",rongyun_token:" + rongyun_token);

                                    LoginUtils.saveMember_avatar(RegisterFirstStepActivity.this,member_avatar);

                                    CommonData.saveLoginData(RegisterFirstStepActivity.this, memberId, registerPhone.getText().toString(), token, rongyun_token);
                                    SharedPreferencesUtil.saveStringPreference(RegisterFirstStepActivity.this, "PREFERENCE", "phone", registerPhone.getText().toString());
                                    SharedPreferencesUtil.saveStringPreference(RegisterFirstStepActivity.this, "PREFERENCE", "password", registerPass.getText().toString());
                                    BaseApplication.getBaseApplication().setJPushAliasAndTag();
                                    startActivity(new Intent(RegisterFirstStepActivity.this, RegisterSecondStepActivity.class));
                                    ProjectGlobalVar.isSendLocation = true;
                                    finish();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                    } else {
                        ToastUtils.showToast("亲，要同意PP优约平台服务协议才能继续哦~");
                    }
                } else {
                    ToastUtils.showToast(isEmpty);
                }
                break;

            /**
             * 跳转协议
             */
            case R.id.register_agreement:
                BaseParam agreementParam = new BaseParam("Public_UserIndex.getSinglePageUrl");
                agreementParam.put("service", "Public_UserIndex.getSinglePageUrl");
                agreementParam.put("article_type", "agreement");
                agreementParam.put("client", "accompany");

                CommonRequestNetData.getJsonObjectData(RegisterFirstStepActivity.this, agreementParam.getParam(), false, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        try {
                            String url = jsonObject.getString("URL");
                            Intent intent = new Intent(RegisterFirstStepActivity.this, BrowserActivity.class);
                            intent.putExtra("url", url);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            case R.id.register_login:
                startActivity(new Intent(RegisterFirstStepActivity.this, LoginActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            default:
                break;
        }
    }
}
